//
//  WMString-Utilities.m
//  Wonderful Moment
//
//  Created by cclv on 11/9/09.
//  Copyright 2009 Ucayee. All rights reserved.
//

#import "WMString-Utilities.h"
#import "NSDate+Utils.h"

#define MINUTES		60
#define HOURS		3600
#define DAYS		86400
#define MONTHS		(86400 * 30)
#define YEARS		(86400 * 30 * 12)

@implementation NSString (WMString_Utilities)

/*!
 @function   string2Date:
 @abstract   Change given string to NSDate
 @discussion If the given str is wrong, should return the current system date
 @param      dateStr A string include the date like "2009-11-09 11:14:41"
 @result     The NSDate object with given format
 */
+ (NSDate *)string2Date:(NSString *)dateStr {
    
    if (10 > [dateStr length]) {
        
        return [NSDate date];
    }
    NSRange range;
    NSString *year, *month, *day, *hr, *mn, *sec;
    range.location = 0;
    range.length = 4;
    year = [dateStr substringWithRange:range];
    
    range.location = 5;
    range.length = 2;
    month = [dateStr substringWithRange:range];
    
    range.location = 8;
    range.length = 2;
    day = [dateStr substringWithRange:range];
    
    if (11 < [dateStr length]) {
        
        range.location = 11;
        range.length = 2;
        hr = [dateStr substringWithRange:range];
    } else {
        hr = @"0";
    }
    
    if (14 < [dateStr length]) {
        
        range.location = 14;
        range.length = 2;
        mn = [dateStr substringWithRange:range];
    } else {
        mn = @"0";
    }
    
    if (17 < [dateStr length]) {
        
        range.location = 17;
        range.length = 2;
        sec = [dateStr substringWithRange:range];
    } else {
        sec = @"0";
    }
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:		[year integerValue]];
    [comps setMonth:	[month integerValue]];
    [comps setDay:		[day integerValue]];
    [comps setHour:		[hr integerValue]];
    [comps setMinute:	[mn integerValue]];
    [comps setSecond:	[sec integerValue]];
    
    NSCalendar *gregorian = [NSCalendar autoupdatingCurrentCalendar];
    NSDate *returnDate = [gregorian dateFromComponents:comps];
    //[comps release];
    return returnDate;
}

/*!
 @function   stringFromCurrent:
 @abstract   Change given date string to "xxx ago" format
 @discussion If the given str is wrong, should return the current date
 @param      dateStr A string include the date like "2009-11-09 11:14:41" at least 10 characters
 @result     The string with "xxx ago", xxx: years, months, days, hours, minutes, seconds
 */
+ (NSString *)stringFromCurrent:(NSString *)dateStr {
    
    NSDate *earlierDate = [NSString string2Date:dateStr];
    
    NSDate *sysDate = [NSDate date];
    //	 //NSLog(@"now from System [%@]", [sysDate description]);
    double timeInterval = [sysDate timeIntervalSinceDate:earlierDate];
    //	 //NSLog(@"[%f]", timeInterval);
    
    NSInteger yearsAgo = timeInterval / YEARS;
    if (1 < yearsAgo) {
        
        return [NSString stringWithFormat:@"%ld 年以前", (long)yearsAgo];
    } else if (1 == yearsAgo) {
        
        return @"1 年以前";
    }
    
    NSInteger monthsAgo = timeInterval / MONTHS;
    if (1 < monthsAgo) {
        
        return [NSString stringWithFormat:@"%ld 月以前", (long)monthsAgo];;
    } else if (1 == monthsAgo) {
        
        return [NSString stringWithFormat:@"1 月以前"];
    }
    
    NSInteger daysAgo = timeInterval / DAYS;
    if (1 < daysAgo) {
        
        return [NSString stringWithFormat:@"%ld 天以前", (long)daysAgo];
    } else if (1 == daysAgo) {
        
        return @"1 天以前";
    }
    
    NSInteger hoursAgo = timeInterval / HOURS;
    if (1 < hoursAgo) {
        
        return [NSString stringWithFormat:@"%ld 小时以前", (long)hoursAgo];
    } else if (1 == hoursAgo) {
        
        return @"1小时以前";
    }
    
    NSInteger minutesAgo = timeInterval / MINUTES;
    if (1 < minutesAgo) {
        
        return [NSString stringWithFormat:@"%ld 分钟以前", (long)minutesAgo];
    } else if (1 == minutesAgo) {
        
        return @"1 分钟以前";
    }
    // 1 sceond ago? we ignore this time
    return [NSString stringWithFormat:@"%ld 秒以前", (long)timeInterval];
}
// 时间转时间戳的方法
+ (NSString *)currentTimeNumber
{
    NSDate *datenow = [NSDate date];
    // NSString *nowtimeStr = [formatter stringFromDate:datenow];
    // 时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}

+ (NSString *)currentTimeStamp
{
    NSDate *datenow = [NSDate date];
    // NSString *nowtimeStr = [formatter stringFromDate:datenow];
    // 时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return [[self class] timeStampAddTimer:timeSp];
}

+ (NSString *)timeStampAddTimer:(NSString *)stamp{
    NSTimeInterval time=[stamp doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [formatter stringFromDate:detaildate];
}

+ (NSString *)dataOrTimer:(NSString *)stamp{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    NSDate* date = [formatter dateFromString:stamp]; //------------将字符串按formatter转成nsdate
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];

    return timeSp;
}
// 时间戳转化为时间字符串 yyyy-MM-dd
+ (NSString *)timeStamp:(NSString *)stamp{
    NSTimeInterval time=[stamp doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    return [formatter stringFromDate:detaildate];
}

// 时间戳转化为时间字符串 yyyy年MM月dd日
+ (NSString *)timeStampChinese:(NSString *)stamp{
    NSTimeInterval time=[stamp doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    
    return [formatter stringFromDate:detaildate];
}

// 时间戳转化为时间字符串 yyyy.MM.dd
+ (NSString *)time2Stamp:(NSString *)stamp
{
    NSTimeInterval time = [stamp doubleValue];
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy.MM.dd"];
    return [formatter stringFromDate:detaildate];
}

//时间戳转化为 MM-dd（需填写）
+ (NSString *)timeModel:(NSString *)model Stamp:(NSString *)stamp
{
    NSTimeInterval time = [stamp doubleValue];
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:model];
    return [formatter stringFromDate:detaildate];
}

+ (NSString *)time3Stamp:(NSString *)stamp
{
    NSTimeInterval time = [stamp doubleValue];
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    return [formatter stringFromDate:detaildate];
}



+ (NSString *)string1FromCurrent:(NSString *)dateStr
{
    NSInteger timeInterval = [dateStr integerValue];
    NSInteger daysAgo = timeInterval / DAYS;
    NSInteger hoursAgo = timeInterval % DAYS / HOURS;
    NSInteger minutesAgo = timeInterval % HOURS / MINUTES;
    NSInteger secondsAgo = timeInterval % MINUTES;
    // 1 sceond ago? we ignore this time
    return [NSString stringWithFormat:@"%ld天%ld小时%ld分钟%ld秒", (long)daysAgo, (long)hoursAgo, (long)minutesAgo, (long)secondsAgo];
    // return [NSString stringWithFormat:@"%d 秒以前", (NSInteger)timeInterval];
}

+ (NSString *)string2FromTimeInterval:(NSInteger)timeInterval
{
    NSInteger daysAgo = timeInterval / DAYS;
    NSInteger hoursAgo = timeInterval % DAYS / HOURS;
    NSInteger minutesAgo = timeInterval % HOURS / MINUTES;
    // NSInteger secondsAgo = timeInterval % MINUTES;
    // 1 sceond ago? we ignore this time
    return [NSString stringWithFormat:@"%ld天%ld时%ld分", (long)daysAgo, (long)hoursAgo, (long)minutesAgo];
}

+ (NSString *)changeTheDateString:(NSString *)str
{
    
    NSTimeInterval time = [str doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
     NSString *subString = [[detaildate description] substringWithRange:NSMakeRange(0, 19)];

    NSDate *lastDate = [NSDate dateFromString:subString withFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *dateStr;  //年月日
    NSString *period;   //时间段
    NSString *hour;     //时
    
    if ([lastDate year] == [[NSDate date] year]) {
        NSInteger days = [NSDate daysOffsetBetweenStartDate:lastDate endDate:[NSDate date]];
        if (days <= 2) {
            dateStr = [lastDate stringYearMonthDayCompareToday];
        } else if (days <= 5) {
            dateStr = [lastDate stringYearMonthWeekdayCompareTodayWithDate:lastDate];
        } else {
            dateStr = [lastDate stringYearMonthDay];
 
        }
    } else {
        dateStr = [lastDate stringYearMonthDay];
    }
    
    BOOL is12hoursFormate = [self is12hoursFormate];
    if ([lastDate hour] < 12) {
        if (is12hoursFormate) {
            period = @"早上";
        } else {
            period = @"";
        }
        hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour]];
    } else {
        if (is12hoursFormate) {
            period = @"下午";
            hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour]-12];
        } else {
            period = @"";
            hour = [NSString stringWithFormat:@"%02d",(int)[lastDate hour]];

        }
    }
    
    return [NSString stringWithFormat:@"%@ %@ %@:%02d",dateStr,period,hour,(int)[lastDate minute]];
}

+ (BOOL)is12hoursFormate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeStyle = NSDateFormatterShortStyle;
    formatter.locale = [NSLocale autoupdatingCurrentLocale];
    NSString *time = [formatter stringFromDate:[NSDate date]];
    NSRange amRange = [time rangeOfString:[formatter AMSymbol]];
    NSRange pmRange = [time rangeOfString:[formatter PMSymbol]];
    if (amRange.location != NSNotFound || pmRange.location != NSNotFound) {
        return YES;
    } else {
        return NO;
    }
}



@end
